package carstore.ui.controllers;

import carstore.ui.models.Car;
import carstore.ui.models.Carstore;
import carstore.ui.models.OrderInfo;
import carstore.ui.models.OrderInfoExt;
import carstore.ui.models.User;
import carstore.ui.services.MainService;
import java.math.BigInteger;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainController {
	
	@Autowired
	private MainService mainservice;
	//   @Autowired
	//   private DiscoveryClient discoveryClient;
	//    private ServiceInstance serviceInstancesByApplicationName(
	//            @PathVariable String applicationName) {
	//        List<InstanceInfo> result = new ArrayList<InstanceInfo>();
	//        result=this.discoveryClient.getInstances(applicationName);
	//        if(result.Length==0)
	//        {
	//            return null;
	//        }
	//        else
	//        {
	//            return result[0];
	//        }
	//            }
	
	//@RequestMapping(value = "/order/book")
	public void redirectToBook(HttpServletResponse httpServletResponse) {
		//System.out.println("redirecting...");
		httpServletResponse.setHeader("Location", "html/book.html");
	}
	
	//@RequestMapping("/alluser")
	public List<User> getall()
	{
		return (List<User>) mainservice.searchAllUsers();
	}
	
	@RequestMapping(value="/allusers")
	public ResponseEntity<List<User>> readAllUsers(){
		List<User> users=mainservice.searchAllUsers();
		return new ResponseEntity<List<User>>(users,HttpStatus.OK);
	}
	
	@RequestMapping(value="/orderserver/allorders")
	public ResponseEntity<List<OrderInfo>> readAllOrders(){
		//System.out.println("MainController.readAllOrders...");
		List<OrderInfo> orders=mainservice.searchAllOrders();
		return new ResponseEntity<List<OrderInfo>>(orders,HttpStatus.OK);
	}
	
	@RequestMapping(value="/storeserver/allstores")
	public ResponseEntity<List<Carstore>> readCarstoreInfo(){
		List<Carstore> stores=mainservice.searchAllCarStores();
		return new ResponseEntity<List<Carstore>>(stores,HttpStatus.OK);
	}
	
	@RequestMapping(value="/userserver/getuserbyid")
	public ResponseEntity<List<User>> readUserInfobyId(int UserId){
		List<User> users=mainservice.getUsersbyUserId(UserId);
		return new ResponseEntity<List<User>>(users,HttpStatus.OK);
	}
	//public ResponseEntity<List<Car>> getCarsbyUserId(){
	@RequestMapping(value="/userserver/carsbyuser")
	public ResponseEntity<List<Car>> getCarsbyUserId(int UserId){
		//System.out.println("MainController.getCarsbyUserId");
		//int UserId=1;
		List<Car> cars=mainservice.getCarsbyUserId(UserId);
		for(int i=0;i<cars.size();i++)
		{
			//System.out.println(cars.get(i).getId()+": "+cars.get(i).getSelected());
			//System.out.println(cars.get(i).toString());
		}
		return new ResponseEntity<List<Car>>(cars,HttpStatus.OK);
	}
	
	@RequestMapping(value="/orderserver/ordersbyuser")
	public ResponseEntity<List<OrderInfoExt>> getOrdersbyUserId(int UserId){
		//System.out.println("MainController.getOrdersbyUserId");
		List<OrderInfoExt> orders=mainservice.getOrdersbyUserId(UserId);
		return new ResponseEntity<List<OrderInfoExt>>(orders,HttpStatus.OK);
	}
	@RequestMapping(value = "/orderserver/addorder", method = RequestMethod.POST)
	@ResponseBody
	//public void setNewOrderInfo(OrderInfo orderinfo){
	public void setNewOrderInfo(BigInteger orderId, String orderTime, int storeId, int carID, int userId, String orderStatus){
		mainservice.setNewOrderInfo(orderId,orderTime,storeId,carID,userId,orderStatus);
	}

	@RequestMapping(value = "/orderserver/paybyorderid", method = RequestMethod.POST)
	@ResponseBody
	public void setPayOrderStatus(BigInteger orderId ){
		mainservice.setPayOrderStatus(orderId);
	}

	@RequestMapping(value = "/userserver/updatecarstatus", method = RequestMethod.POST)
	@ResponseBody
	public void setCarSelectStatus(Integer UserId,Integer CarId){
		//System.out.println("ui.MainController.setCarSelectStatus");
		mainservice.setCarSelectStatus(UserId,CarId);
	}
}
